package com.xjy.ftpmanagment.action;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.xjy.common.action.BaseAction;
import com.xjy.common.util.PageBean;
import com.xjy.ftpmanagment.command.SustatConstant;
import com.xjy.ftpmanagment.pojo.StatFile;
import com.xjy.ftpmanagment.pojo.StatIp;
import com.xjy.ftpmanagment.pojo.StatUser;
import com.xjy.ftpmanagment.service.StatService;
import com.xjy.ftpmanagment.service.StatUserService;
import com.xjy.ftpmanagment.util.Page;

public class StatAction extends BaseAction implements ServletRequestAware{
	private static final long serialVersionUID = 5424100177972418471L;
	private static final Log logger = LogFactory.getLog(StatAction.class);
	
	private int page;
	private int pageSize = 10;
	private PageBean pageBean = new PageBean();
	
	private StatUserService statUserService;
	private StatService statService;
	
	private String startTime;		// 查询开始时间
	private String endTime;			// 查询结束时间
	private int statType = 0;		// 查询类型
	
	private final int TOP_NUM = 5;	// 饼图统计数目，超过数目的合并为"other"
	private Map chartData;			// 下载文件大小饼图的数据集
	private Map chartUpData;		// 上传文件大小饼图的数据集
	
	public String userList() {
		Timestamp now = new Timestamp(System.currentTimeMillis());
		// 默认显示前一天的查询结果
		if (null == startTime) {
			startTime = SustatConstant.getDay(now, -1);
		}
		if (null == endTime) {
			endTime = SustatConstant.getDay(now, -1);
		}
		request.setAttribute("statType", statType);
		request.setAttribute("startTime", startTime);
		request.setAttribute("endTime", endTime);
		
		startTime = startTime + " 00:00:00";
		endTime = endTime + " 23:59:59";
		
		String condition = "statTime > '" + startTime + "' and statTime < '" + endTime + "'";
		List list;
		String result = Page.STAT_USER;
		switch (statType) {
		case SustatConstant.TYPE_STAT_FILE:
			pageBean = statService.getStatFileDao().queryForPage(page, pageSize, condition);
			list = pageBean.getList();
			genMapFile(list);
			result = Page.STAT_FILE;
			break;
		case SustatConstant.TYPE_STAT_IP:
			pageBean = statService.getStatIpDao().queryForPage(page, pageSize, condition);
			list = pageBean.getList();
			genMapIp(list);
			result = Page.STAT_IP;
			break;
		default:
			pageBean = statUserService.queryForPageDate(page, pageSize, condition);
			list = pageBean.getList();
			genMapUser(list);
			result = Page.STAT_USER;
		}
		for(Object o : list) {
			// logger.info(o.toString());
		}
		return result;
	}
	
	// 按文件统计，文件下载次数的chart数据
	private void genMapFile(List list) {
		chartData = new HashMap();
		List<String> keyList = new ArrayList<String>();
		List<Integer> valueList = new ArrayList<Integer>();
		int i = 0;
		int otherCount = 0;
		for(Object o : list) {
			StatFile po = (StatFile)o;
			if(i < TOP_NUM) {
				// 文件路径中的\会使得前台饼图的xml报错
				keyList.add(po.getFileName().replaceAll("\\\\", "/"));
				valueList.add(po.getFileCount());
			} else {
				otherCount += po.getFileCount();
			}
			i++;
		}
		if(otherCount > 0) {
			keyList.add("其他");
			valueList.add(otherCount);
		}
		chartData.put("key", keyList);
		chartData.put("value", valueList);
	}
	
	// 按IP统计，下载文件大小chart数据
	private void genMapIp(List list) {
		chartData = new HashMap();
		List<String> keyList = new ArrayList<String>();
		List<Double> valueList = new ArrayList<Double>();
		int i = 0;
		double otherMb = 0.0d;
		for(Object o : list) {
			StatIp po = (StatIp)o;
			if(i < TOP_NUM) {
				keyList.add(po.getIp());
				valueList.add(po.getDownMb());
			} else {
				otherMb += po.getDownMb();
			}
			i++;
		}
		if(otherMb > 0) {
			keyList.add("其他");
			valueList.add(otherMb);
		}
		chartData.put("key", keyList);
		chartData.put("value", valueList);
	}

	// 按用户统计，下载文件大小chart数据
	@SuppressWarnings("unchecked")
	private void genMapUser(List list) {
		chartData = new HashMap();
		List<String> keyList = new ArrayList<String>();
		List<Double> valueList = new ArrayList<Double>();
		int i = 0;
		double otherMb = 0.0d;
		for(Object o : list) {
			StatUser po = (StatUser)o;
			if(i < TOP_NUM) {
				keyList.add(po.getUser());
				valueList.add(po.getDownMb());
			} else {
				otherMb += po.getDownMb();
			}
			i++;
		}
		if(otherMb > 0) {
			keyList.add("其他");
			valueList.add(otherMb);
		}
		chartData.put("key", keyList);
		chartData.put("value", valueList);
	}

	public void setPage(int page) {
		this.page = page;
	}

	public int getPage() {
		return page;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageBean(PageBean pageBean) {
		this.pageBean = pageBean;
	}

	public PageBean getPageBean() {
		return pageBean;
	}

	public void setStatUserService(StatUserService statUserService) {
		this.statUserService = statUserService;
	}

	public StatUserService getStatUserService() {
		return statUserService;
	}

	public StatService getStatService() {
		return statService;
	}

	public void setStatService(StatService statService) {
		this.statService = statService;
	}

	public void setStartTime(String startTime) {
		this.startTime = startTime;
	}

	public String getStartTime() {
		return startTime;
	}

	public void setEndTime(String endTime) {
		this.endTime = endTime;
	}

	public String getEndTime() {
		return endTime;
	}

	public void setStatType(int statType) {
		this.statType = statType;
	}

	public int getStatType() {
		return statType;
	}

	public void setChartData(Map chartData) {
		this.chartData = chartData;
	}

	public Map getChartData() {
		return chartData;
	}

	public void setChartUpData(Map chartUpData) {
		this.chartUpData = chartUpData;
	}

	public Map getChartUpData() {
		return chartUpData;
	}
}
